﻿using System;
using System.Windows.Forms;
using SdmxMl.Structure;

namespace SdmxMl.UC
{
    public partial class UcTextFormat : UserControl
    {
        private TextFormatType tf;
        private bool _isFinal;

        public bool IsFinal
        {
            get { return _isFinal; }
            set
            {
                _isFinal = value;
                UpdateControls();

            }
        }

        public TextFormatType TF
        {
            get 
            {
                UpdateTfDetail();
                return tf; 
            }
            set
            {
                tf = value;
                UpdateControls();
            }
        }

        /// <summary>
        /// Update TF with controls detail content
        /// </summary>
        private void UpdateTfDetail()
        {
            if (tf != null && !_isFinal)
            {
                tf.minLength = textBoxMinLen.Enabled ? textBoxMinLen.Text : string.Empty;
                tf.maxLength = textBoxMaxLen.Enabled ? textBoxMaxLen.Text : string.Empty;
                tf.pattern = textBoxPattern.Enabled ? textBoxPattern.Text : string.Empty;
                tf.startValue = textBoxStartVal.Enabled ? textBoxStartVal.Text : string.Empty;
                tf.endValue = textBoxEndVal.Enabled ? textBoxEndVal.Text : string.Empty;
                tf.decimals = textBoxDecimal.Enabled ? textBoxDecimal.Text : string.Empty;
                tf.interval = textBoxInterval.Enabled ? textBoxInterval.Text : string.Empty;
                tf.isSequence = checkBoxIsSequence.Enabled ? checkBoxIsSequence.Checked : false;
                tf.timeInterval = textBoxTimeInt.Enabled ? textBoxTimeInt.Text : string.Empty;
                tf.isMultiLingual = checkBoxIsMultiLingual.Enabled ? checkBoxIsMultiLingual.Checked : false;
            }
        }

        private void UpdateControls()
        {
            if (tf != null)
            {
                this.comboBoxType.SelectedIndex = (int)tf.TextType;
                this.comboBoxType.Enabled = _isFinal == false;
                UpdateOtherControls();
            }            
        }

        private void UpdateOtherControls()
        {
            TextTypeBoxes tbEnable = tf.GetBoxEnable(tf.TextType);
            if (tbEnable.isMultiLingual && tf.TextType == TextTypeType.String)
            {
                checkBoxIsMultiLingual.Checked = tf.isMultiLingual;
                checkBoxIsMultiLingual.Enabled = _isFinal == false;
            }
            else
            {
                checkBoxIsMultiLingual.Checked = false;
                checkBoxIsMultiLingual.Enabled = false;
                tf.isMultiLingual = false;
            }

            if (tbEnable.isSeq)
            {
                checkBoxIsSequence.Checked = tf.isSequence;
                checkBoxIsSequence.Enabled = true;
            }
            else
            {
                checkBoxIsSequence.Checked = false;
                checkBoxIsSequence.Enabled = false;
            }

            TextBoxState(textBoxMinLen, tbEnable.isMinLen, tf.minLength);
            TextBoxState(textBoxMaxLen, tbEnable.isMaxLen, tf.maxLength);
            TextBoxState(textBoxInterval, tbEnable.isInterval, tf.interval);
            TextBoxState(textBoxDecimal, tbEnable.isDecimal, tf.decimals);
            TextBoxState(textBoxStartVal, tbEnable.isStart, tf.startValue);
            TextBoxState(textBoxEndVal, tbEnable.isEnd, tf.endValue);
            TextBoxState(textBoxPattern, tbEnable.isPattern, tf.pattern);
            TextBoxState(textBoxTimeInt, tbEnable.isTimeInt, tf.timeInterval);

            switch (tf.TextType)
            {
                case TextTypeType.Decimal:
                    textBoxStartVal.InputType = NumEditType.Decimal;
                    textBoxEndVal.InputType = NumEditType.Decimal;
                    break;

                case TextTypeType.Float:
                case TextTypeType.Double:
                    textBoxStartVal.InputType = NumEditType.Double;
                    textBoxEndVal.InputType = NumEditType.Double;
                    break;

                case TextTypeType.Integer:
                case TextTypeType.Long:
                case TextTypeType.Short:
                    textBoxStartVal.InputType = NumEditType.LargeInteger;
                    textBoxEndVal.InputType = NumEditType.LargeInteger;
                    break;
                default:
                    textBoxStartVal.InputType = NumEditType.AlphaNum;
                    textBoxEndVal.InputType = NumEditType.AlphaNum;
                    break;
            }
        }

        /// <summary>Update textbox according to text type </summary>
        /// <param name="tb"></param>
        /// <param name="enabled"></param>
        /// <param name="val"></param>
        private void TextBoxState(TextBox tb, bool enabled, string val)
        {
            bool stateEnabled = enabled;
            if (_isFinal)
                enabled = false;

            if (!enabled)
            {
                tb.Enabled = false;
                tb.Text = stateEnabled ? val: string.Empty;
            }
            else
            {
                tb.Enabled = true;
                tb.Text = val;
            }

        }


        public UcTextFormat()
        {
            InitializeComponent();
            foreach (TextTypeType t in Enum.GetValues(typeof(TextTypeType)))
                comboBoxType.Items.Add(t.ToString());

        }

        private void comboBoxType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tf != null)
            {
                // Default to multilingual when string selected
                bool wasString = tf.TextType == TextTypeType.String;

                tf.TextType = (TextTypeType)this.comboBoxType.SelectedIndex;

                if (tf.TextType == TextTypeType.String && wasString == false)
                    tf.isMultiLingual = true;

                UpdateOtherControls();
            }

        }

        private void checkBoxIsMultiLingual_CheckedChanged(object sender, EventArgs e)
        {
            if (tf != null)
            {
                tf.isMultiLingual = checkBoxIsMultiLingual.Checked && tf.TextType == TextTypeType.String;
            }
        }
    }
}
